package com.jin.xszs;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.OutputStream;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static android.content.ContentValues.TAG;

public class hook implements IXposedHookLoadPackage {
    private OutputStream os;
    boolean isOk = false;
    boolean isOk1 = true;
    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam)throws Throwable{
        if (!lpparam.packageName.equals("com.helper.readhelper")){
            return;
        }
        XposedBridge.log("Hook主程序");
        XposedHelpers.findAndHookMethod("com.security.shell.AppStub1", lpparam.classLoader, "attachBaseContext", Context.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                Context context =(Context)param.args[0];
                ClassLoader classLoader = context.getClassLoader();
                XposedBridge.log("Hook 加固上下文");
                XposedHelpers.findAndHookMethod("com.helper.readhelper.activity.SplashActivity", classLoader, "a", Context.class, String.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        XposedBridge.log("Hook[SplashActivity]"+param.args[1].toString()+":"+param.getResult());
                        param.setResult(false);
                    }
                });

                XposedHelpers.findAndHookMethod("com.helper.readhelper.g.y", classLoader, "e", Context.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        XposedBridge.log("Hook[e] :"+param.getResult());
                        param.setResult(false);
                    }
                });

                //Hook提交按钮坐标位置
                XposedHelpers.findAndHookMethod("com.helper.readhelper.window.WindowUtilsMoney", classLoader, "getRandom", int.class, int.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        XposedBridge.log(param.getResult().toString());
                        param.setResult(10);
                    }
                });

                //HOOK刷乐检测无障碍
                XposedHelpers.findAndHookMethod("com.helper.readhelper.view.q", classLoader, "p", new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        XposedBridge.log(param.getResult().toString());
                        param.setResult(true);
                    }
                });

                //Hook刷乐当前状态

                XposedHelpers.findAndHookMethod("com.helper.readhelper.window.WindowUtilsMoney", classLoader, "updateView", String.class, String.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        XposedBridge.log(param.args[1].toString());
                        String a = param.args[1].toString();
                        Log.d(TAG, "afterHookedMethod: "+isOk1);
                        switch (a){
                            case "点击《去看看》，进入任务页。":
                                if (isOk1){
                                    click(612,1072,1000);
                                    isOk1 = false;
                                    isOk =true;
                                }
                                break;
                            case "已进入任务页，请点击关注，并在倒计时结束后，提交任务。":
                               if(isOk){
                                   click(720,605,1000);
                                   isOk =false;
                                   isOk1=true;
                               }
                                break;
                        }
                        //tap(100,100);
                    }
                });
            }
        });
    }

    public final void exec(String cmd){
        try{
            if(os ==null){
                os = Runtime.getRuntime().exec("su").getOutputStream();
            }
            os.write(cmd.getBytes());
            os.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public final void tap(int x,int y){
        exec("input tap " + x + " "+y+"\n");
    }

    public void click(final int x, final int y, int delayMillis){
        new Handler().postDelayed(new Runnable(){
            public void run(){
                //execute the task
                tap(x,y);
            }
        },delayMillis);
    }
}
